package org.drl.tvkit.widget;

import java.io.File;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.drl.tvkit.KitApplication;
import org.drl.tvkit.util.L;
import org.drl.tvkit.util.UrlFormatUtil;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.utils.MemoryCacheUtils;

@SuppressLint("HandlerLeak")
public class ShadowManagerImpl extends ShadowManager implements Runnable {
	public static final class Data {
		public MovieView mSrcView;
		public ShadowView mShadowView;
		public String mShadowFile;
	}
	private final BlockingQueue<Data> mQueue;
	public void quit() {
		try {
			mQuit = true;
			mThread.interrupt();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		L.d(TAG, "quit");
	}
	private volatile boolean mQuit = false;
	String TAG = "ShadowManagerImpl";
	private File mFile;
	private Thread mThread;
	private ImageLoader mImageLoader;
	public ShadowManagerImpl(Context _ctx) {
		super(_ctx);
		mThread = new Thread(this);
		if(KitApplication.getInstance()!=null){
			mImageLoader = KitApplication.getInstance().getImageLoader();
		}
		mQueue = new LinkedBlockingQueue<Data>();
		mFile = new File("");
		try{
		File _f = getContext().getCacheDir();
		mFile = new File(_f.toString()+File.separator+"shadows");
			if(!mFile.exists()){
				mFile.mkdirs();
			}
		}catch(Exception ex){
			
		}
		mThread.start();
	}
	@Override
	public void removeShadowCacheFile(MovieView _movieView) {
		File _del = new File(mFile,_movieView.getId()+".png");
		if(_del.exists()){
			_del.delete();
			L.i(TAG,"-->del file-->"+_del.toString());
		}
	}
	@Override
	public synchronized void run() {
		while (true) {
			Data _data;
			try {
				_data = mQueue.take();
			} catch (InterruptedException e) {
				if (mQuit) {
					return;
				}
				continue;
			}
			if(_data!=null){
				loadShadowFile(_data);
			}
		}
	}
	private synchronized void loadShadowFile(final Data _data){
		if (_data != null) {
			File _dir = new File(mFile.toString());
			if (!_dir.exists()) {
				_dir.mkdirs();
			}
			String _name = _data.mSrcView.getShadowFileName();
			final File _file = new File(_dir,_name);
			Log.i("MovieView", _file.toString());
			MemoryCacheUtils.removeFromCache(UrlFormatUtil
					.formatUrlFile(_file.toString()),
					mImageLoader.getMemoryCache());
			if (!_file.exists()) {
				int _width = _data.mSrcView.getWidth();
				int _height = _data.mSrcView.getHeight();
				if(_width<=0||_height<=0){
					try {
						wait(3000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				if(_width<=0||_height<=0){
					mQueue.add(_data);
					return;
				}
				ShadowUtil.outBitmapShadowFile(
						_data.mSrcView.getDrawingCache(), _data.mSrcView,
						_data.mShadowView, _file);
			}
			_data.mShadowFile = _file.toString();
			Message _msg = mHandler.obtainMessage(LOAD_SHADOW_FILE);
			_msg.obj = _data;
			mHandler.sendMessage(_msg);
		}
	}
	private final int LOAD_SHADOW_FILE =2;
	android.os.Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			switch(msg.what){
			case LOAD_SHADOW_FILE:
				if(msg.obj!=null){
					Data _data = (Data) msg.obj;
					_data.mSrcView.loadShadowAsyn(_data.mShadowFile);
				}
				break;
			}
		}
	};
	@Override
	public void loadShadowAsyn(MovieView _movieView, ShadowView _shadow) {
		Data _data = new Data();
		_data.mSrcView = _movieView;
		_data.mShadowView = _shadow;
		mQueue.add(_data);
	}
}
